package com.itboye.system.helper;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

/**
 * 系统配置信息获取帮助类
 * Created by hebidu on 16/5/10.
 */
public class ByConfigHelper {

    /**
     *
     * 获取允许上传文件的后缀
     * @return 字符串数组
     */
    public String[] getUploadFileAllowExts(){
        String allowExts = this.read("upload_file_ext");
        System.out.println(allowExts);
        return allowExts.split(",");
    }

    /**
     * 获取上传文件最大大小
     * @return
     */
    public int getUploadFileMaxSize(){
        String maxSize = this.read("upload_file_maxsize");
        if(maxSize == null || maxSize.length() == 0){
            return  0;
        }
        int size = Integer.parseInt(maxSize);
        return size;
    }


    /**
     * 返回本项目访问的图片url地址
     * @return
     */
    public String getImgSiteUrl(){
        return this.read("img_site_url");
    }

    /**
     * 返回本项目访问的url地址
     * @return
     */
    public String getSiteUrl(){
        return this.read("site_url");
    }

    /**
     * 返回文件上传路径相对于webapp文件夹
     * @return
     */
    public String getUploadFilePath(){
        return this.read("upload_file_path");
    }

    /**
     * 返回发生解密、加密函数异常时,默认返回的信息
     * @return
     */
    public String getDefaultMsg(){
        return this.read("default_msg");
    }

    /**
     * 是否开发环境
     * @return
     */
    public Boolean isDevelopment(){

        String env =  this.read("environment");
        return env.equalsIgnoreCase("development");

    }

    /**
     * 读取密码混淆字符串
     * @return
     */
    public String getPasswordSalt(){
        return this.read("password_salt");
    }

    /**
     * 环境类型读取
     */
    public String getEnvironment(){
        return this.read("environment");
    }

    /**
     * mybatis 配置文件名
     */
    public static final String MyBatisConfigName = "mybatis-config.xml";

    private static HashMap<String,String> returnData;

    private Properties prop;

    private static HashMap<String,String> map;

    private static class LazyConfigHolder {
        public static ByConfigHelper instace = new ByConfigHelper();
    }

    public static ByConfigHelper getInstance() {
        return LazyConfigHolder.instace;
    }

    private ByConfigHelper(){
        this.loadConfig();
    }

    /**
     * 载入配置信息
     * @since  1.6
     */
    private void loadConfig(){
        try {

            prop = new Properties();
            map = new HashMap<String,String>();

            InputStream inStream;

            inStream = this.getClass().getResourceAsStream("/config.properties");

            prop.load(inStream);
            Set<String> propNames = prop.stringPropertyNames();
            Iterator<String> it = propNames.iterator();

            while(it.hasNext()){
                String key = it.next();
                map.put(key, prop.getProperty(key));
            }

            inStream.close();



        } catch (FileNotFoundException e) {

            e.printStackTrace();
            prop = null;
        } catch (IOException e) {
            e.printStackTrace();
            prop = null;
        }
    }


    /**
     * 读取
     * @param key
     * @return 空字符串
     */
    public String read(String key){

        if(ByConfigHelper.map == null){
            loadConfig();
        }

        if(ByConfigHelper.map == null){
            return "";
        }

        String value = ByConfigHelper.map.get(key);

        if(!key.equals("environment") && value == null) {
            String prefix = this.getEnvironment();
            value = ByConfigHelper.map.get(prefix + "." +  key);
        }

        return value;

    }

}
